package com.lin.missyou.api.v1;

import com.lin.missyou.dto.TokenDTO;
import com.lin.missyou.dto.TokenGetDTO;
import com.lin.missyou.exception.http.NotFoundException;
import com.lin.missyou.service.WxAuthenticationService;
import com.lin.missyou.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gcq
 * @Create 2021-10-28
 */
@RestController
@RequestMapping("/token")
public class TokenController {


    @Autowired
    WxAuthenticationService wxAuthenticationService;

    /**
     * @Validated 开启注解功能
     * @param tokenGetDTO
     * @return
     */
    @PostMapping
    public Map<String, String> getToken(@RequestBody @Validated TokenGetDTO tokenGetDTO)
    {
        Map<String, String> map = new HashMap<>();
        String token = "";
        switch (tokenGetDTO.getType()) {
            case USER_WX:
                token = wxAuthenticationService.code2Session(tokenGetDTO.getAccount());
                break;
            case USER_Email:
                break;
            default:
                throw new NotFoundException(10003);
        }
        map.put("token", token);
        return map;
    }

    /**
     * 检查token是否还生成
     * @param token
     * @return
     */
    @PostMapping("/verify")
    public Map<String, Boolean> verify(@RequestBody TokenDTO token)
    {
        Map<String, Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(token.getToken());
        map.put("is_valid", valid);
        return map;
    }
}